package blog.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.ArticleService;

import blog.entity.Article;
import blog.entity.Category;
import blog.entity.Tag;
import blog.mapper.ArticleMapper;

@Service
public class ArticleServiceImpl implements ArticleService {
	@Autowired
	private ArticleMapper articleMapper;
	
	/**
	 * 	查询最近的文章
	 */
	public List<Article> listRecentArticle(Integer n) {
		return articleMapper.listRecentArticle(n);
	}
	
	/**
	 * 	分页查询所有文章
	 */
	public PageInfo<Article> getPageArticleList(Integer pageIndex, Integer pageSize) {
		//自动的对本方法下的第一个sql 查询进行分页
		PageHelper.startPage(pageIndex,pageSize);
		//紧跟着的第一个select 方法会被分页
		List <Article> articleList=articleMapper.findAll();
		//查出每个文章对应的分类信息
		for(Article article:articleList) {
			//文章对应的分类信息
			List<Category> categoryList = getCategoryByArtice(article);
			//将分类信息放在每个文章中
			article.setCategoryList(categoryList);
		}
		//包装查出来的列表
		return  new  PageInfo<Article> (articleList);
	}
	
	/**
	 * 	添加草稿
	 */
	@Override
	public int insertDraftSubmit(Article article) {
		return articleMapper.insertDraftSubmit(article);
	}

	/**
	 * 	添加文章
	 */
	@Override
	public void addArticle(Article article) {
		System.err.println("执行添加前"+article.getArticleId());
		//往文章表中添数据
		articleMapper.addArticle(article);
		System.err.println("执行添加后"+article.getArticleId());
		//往文章分类表中添数据
		List<Category>categoryList=article.getCategoryList();
		for(Category c: categoryList) {
			articleMapper.addArticleCategory(article.getArticleId(),c.getCategoryId());
		}
		
		//往文章标签表中添数据
		List<Tag> tagList=article.getTagList();
		for(Tag t:tagList) {
			articleMapper.addArticleTag(article.getArticleId(),t.getTagId());
		}
	}

	/**
	 * 	获取文章信息
	 */
	@Override
	public Article getArticleById(int articleId) {
		//查询文章id对应的文章
		Article article = articleMapper.getArticleById(articleId);
		//查询文章对应的分类
		List<Category> categoryList=getCategoryByArtice(article);
		//查询文章对应的标签
		List<Tag> tagList =getTagByArtice(article);
		
		//将分类信息放在每个文章中
		article.setCategoryList(categoryList);
		//将标签信息放在每个文章中 
		article.setTagList(tagList);
		
		return article;
	}
	
	
	/**
	 * 	查询文章对应的标签 
	 * @param article
	 * @return
	 */
	private List<Tag> getTagByArtice(Article article){
		// 查询每个文章id对应的标签id
		List<Integer> tagIdList = articleMapper.getTagListId(article.getArticleId());
		// 文章对应的分类信息
		List<Tag> tagList = null;
		if(tagIdList.size()>0) {
			tagList = articleMapper.getTagList(tagIdList);
		}
		return tagList;
	}
	
	/**
	 * 	查询文章对应的分类
	 * @param article
	 * @return
	 */
	private List<Category> getCategoryByArtice(Article article){
		//查询每个文章id对应的分类id
		List<Integer> categoryIdList =articleMapper.getCategoryListId(article.getArticleId()) ;
		//文章对应的分类信息
		List<Category> categoryList = null;
		//如果查询到了，再根据分类id查询对应的分类
		if(categoryIdList.size()!=0) {
			categoryList = articleMapper.getCategoryList(categoryIdList.get(0),categoryIdList.get(1));
		//如果没有查询到，用	"大分类" 和 "小分类" 代替
		}else {
			categoryList = new ArrayList<Category>();
			Category cate1 = new Category();
			cate1.setCategoryName("大分类");
			Category cate2 = new Category();
			cate2.setCategoryName("小分类");
			categoryList.add(cate1);
			categoryList.add(cate2);
		}
		return categoryList;
	}

	/**
	 * 	修改文章
	 */
	@Override
	public void updateArticle(Article article) {
		//修改文章内容
		articleMapper.updateArticle(article);
		// 修改文章分类映射表中数据
		List<Category> categoryList = article.getCategoryList();
		//修改前先删除
		articleMapper.deleteArticleCategory(article.getArticleId());
		for (Category c : categoryList) {
			//再添加
			articleMapper.addArticleCategory(article.getArticleId(), c.getCategoryId());
		}
		// 修改文章标签表映射中数据
		List<Tag> tagList = article.getTagList();
		//修改前先删除
		articleMapper.deleteArticleTag(article.getArticleId());
		for (Tag t : tagList) {
			//再添加 
			articleMapper.addArticleTag(article.getArticleId(), t.getTagId());
		}
	}
	
	/**
	 * 	删除文章
	 */
	@Override
	public int deleteArticleById(Integer id) {
		//删除文章前，删除文章对应的分类、标签映射关系
		articleMapper.deleteArticleCategory(id);
		articleMapper.deleteArticleTag(id);
		//删除文章
		return articleMapper.deleteArticleById(id);
	}
}
